#!/usr/bin/env python
# coding=utf-8


import os

import argparse
from PIL import Image

_APP = "Icant"
_VER = "0.2"




class profile:
    FILENAMETEMPLATE = "icon_{SIZE}x{SIZE}{DOUBLE}.png"
    def __init__(self, size, double=False):
        if double:
            self.size = (size * 2, size * 2)
            self.filename = self.FILENAMETEMPLATE.format(SIZE=size, DOUBLE="@2x")
        else:
            self.size = (size,size)
            self.filename = self.FILENAMETEMPLATE.format(SIZE=size, DOUBLE='')


PROFILELIST = [
profile(512, True),
profile(512),
profile(256, True),
profile(256),
profile(128, True),
profile(128),
profile(32, True),
profile(32),
profile(16, True),
profile(16)
]




def main():
    """
    设置命令行参数
    """
    parser = argparse.ArgumentParser(description="帮你把一个全尺寸的png图标转换成苹果的iconset目录。")
    parser.add_argument('imageFileName', metavar='IMAGEFILENAME', type=str, help="选择原始的图片文件。")
    parser.add_argument('-o', '--output', dest="outputFolderName", action='store', help="指定输出文件名。")
    parser.add_argument('-v', '--version', action='version', version=_APP+' '+_VER)


    args = parser.parse_args()

    if args.outputFolderName:
        targetFolder = os.path.abspath(args.outputFolderName)
        if os.path.isdir(targetFolder)==False:
            os.mkdir(targetFolder)
    else:
        targetFolder = os.path.abspath(".")

    image = Image.open(args.imageFileName)

    for p in PROFILELIST:
        newImage = image.resize(p.size)
        newImage.save(os.path.join(targetFolder, p.filename))


if __name__ == '__main__':
    main()
